********************************************************************************
*Replication sctipt for: Sending, Ole Jacob, Neil Ketchley, and Morten S. Andersen. Forthcoming. "Ambassador, you're really spoiling us!'' Diplomatic Gifts and Profligate Autocrats, Review of International Organizations
********************************************************************************

cd "C:\Users\neilke\Dropbox\gifts\RIO_replication"
set more off
clear

log using "sending_ketchley_andersen_RIO_log.txt", replace text

********************************************************************************
*Packages to install
********************************************************************************
*ssc install mltl2scatter
*ssc install reghdfe
*ssc install mrobust
*ssc install sensemakr
********************************************************************************

********************************************************************************
*Bring in data frame after random forest MI for missingness on covariates
********************************************************************************

import delimited "C:\Users\neilke\Dropbox\gifts\RIO_replication\master_dataset_RF_MI.csv", bindquote(strict) clear 

********************************************************************************
*Label variables
********************************************************************************

label variable country_name "Country name"
label variable country_id "Country ID"
label variable value_adj "Value of diplomatic gift in adjusted $"
label variable ln_value_adj "Value of diplomatic gift in adjusted $ (log)"
label variable log10_value_adj "Value of diplomatic gift in adjusted $ (log10)"
label variable v2x_polyarchy "Electoral democracy"
label variable years_into_presidency "Years into presidency"
label variable trump "Trump"
label variable state_visit "State visit"
label variable president_abroad "President abroad"
label variable head_of_state "Head of State"
label variable saudi "Saudi Arabia"
label variable v2x_neopat "Corruption/clientalism"
label variable v2x_corr "Political corruption in govt index"
label variable v2xlg_legcon "Legislative constraints on the executive index"
label variable v2mecrit "Print/broadcast media critical of govt"
label variable v2pepwrses "Power distributed by socioeconomic position"
label variable e_migdppcln "GDP per capita (log10)"
label variable eman_dist "Cultural similarity"
label variable nato_member "NATO member"
label variable year "Year"
label variable e_regionpol "World Region"

*************************************************************
*Macros for models
*************************************************************

*DV
global dv ln_value_adj

*Polyarchy
global polyarchy v2x_polyarchy

*Controls
global controls years_into_presidency ///
		trump ///
		state_visit ///
		president_abroad ///
		head_of_state ///
		saudi ///
		v2x_neopat ///
		e_migdppcln ///
		nato_member ///
		eman_dist


********************************************************************************
*Figure 2
********************************************************************************

*max U.S. gift on log 10 scale
di log10(2000)

graph hbox log10_value_adj if value_adj, ylab(1 "10" 2 "100" 3.30102999566 "Max. U.S. gift" 3 "1,000" 4 "10,000" 5 "100,000" 6 "1,000,000", labsize(small) angle(45)) ylab(, nogrid) ytitle(Gift value ($)) yscale(lstyle(none)) box(1, bcolor(white) blcolor(blue)) marker(1, mcolor(blue) msymbol(O) mlcolor(blue) msize(small)) medtype(marker) medmarker(msymbol(Dh) mcolor(blue)) yline(3.30103, lcolor(red) lpattern(dash)) scheme(lean1)

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig2.png", as(png) width(6400) height(4000) replace

*Gift values
sum value_adj, det
	

********************************************************************************
*Figure 3
********************************************************************************

*Calculate average of log10_value_adj and v2x_polyarchy by country
mltl2scatter log10_value_adj v2x_polyarchy, l2id(country_name) labels lfit keepvars

twoway ///
    (scatter log10_value_adj_mlt v2x_polyarchy_mlt, mlabel(L2ID_mlt) mlabsize(tiny) msize(tiny)) ///
    (lfitci log10_value_adj_mlt v2x_polyarchy_mlt, lcolor(blue) fcolor(gs12%60) lpattern(solid)) ///
    (lowess log10_value_adj_mlt v2x_polyarchy_mlt, lcolor(red) lpattern(dash)), ///
    legend(off) ytitle("Average gift value ($, log 10)") xtitle("Average Electoral Democracy score") aspectratio(1) scheme(lean1)

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig3.png", as(png) width(6400) height(4000) replace

*Mean value of Saudi gifts
sum value_adj if country_name == "Saudi Arabia", det
	
********************************************************************************
*Footnote 4 - within country, over time variation
********************************************************************************

xtset country_id
xtsum v2x_polyarchy	
	

********************************************************************************
*Table 1 - multilevel and fixed effects models
********************************************************************************

*Model 1 - bivariate
mixed $dv $polyarchy || year: || country_id: || gift_giver_id:, cformat(%9.2f)
	
*Model 2 - controls
mixed $dv $polyarchy $controls || year: || country_id: || gift_giver_id:, cformat(%9.2f)
	
*Model 3 - year fixed effect
reghdfe $dv $polyarchy $controls, absorb(year) vce(cluster country_id gift_giver_id) cformat(%9.2f)

*Model 4 - year and region fixed effect
reghdfe $dv $polyarchy $controls, absorb(year e_regionpol) vce(cluster country_id gift_giver_id) cformat(%9.2f)
	
********************************************************************************
*Figure 4 - margins from model 2
********************************************************************************
	
quietly mixed $dv $polyarchy $controls || year: || country_id: || gift_giver_id:, cformat(%9.2f)
	
*marginal effects
sum $polyarchy, det
local p50 = r(p50)
local p95 = r(p95)
local p05 = r(p5)

*In text description - 5th percentile =  2089.498; 95th percentile = 825.4552
margins, at($polyarchy=(`p05')) expression(exp(predict(xb))) atmeans
margins, at($polyarchy=(`p95')) expression(exp(predict(xb))) atmeans
margins, at($polyarchy=(`p50')) expression(exp(predict(xb))) atmeans

margins, at($polyarchy=(0(.1)1)) expression(exp(predict(xb))) atmeans	

marginsplot, ///
    ///
    plot1opts(lcolor(blue) lwidth(medthick)) ///
    ci1opts(recast(rarea) fcolor(gs12%60) lcolor(gs8)) ///
    ///
	ytitle("Gift value ($)") ///
	xline(`p50', lcolor(red)) ///
	xline(`p05', lcolor(red)) ///
	xline(`p95', lcolor(red)) ///
	yline(1095.682, lcolor(red)) ///
	addplot(hist $polyarchy, /// 
	xlab(,grid) ylab(,grid) ///
	discrete /// 
	yaxis(2) /// 
	yscale(alt lcolor() axis(2)) /// 
ylabel(0 " " 20 " " 40 " " 60 " " 80 " " 100 " ", /// 
labcolor() axis(2) tlcolor(black) tlwidth(thin) labsize(small) tl(0)) /// for 2nd axis
ytitle(" ", axis(2)) plotregion(margin(b = 0))) legend(off) title("") scheme(lean1)

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig4.png", as(png) width(6400) height(4000) replace


********************************************************************************
*Figure 5a & 5b - specificiation curves (beta and p-values)
********************************************************************************
	
*specification curve
encode e_regionpol, gen(e_regionpol_id)

mrobust reg $dv $polyarchy $controls i.year i.e_regionpol_id, vce(cluster country_id) saveas(spec_curve)

preserve
use spec_curve, clear
kdensity b_v2x_polyarchy, xlab(-2 -1.5 -1 -.5 0 0.5,grid) ylab(,grid) title("") xtitle("{&beta} Electoral democracy index") ytitle("Density of coefficients") xline(0, lcolor(red)) xscale(r(., 0.5)) note("") bw(0.1) aspectratio(1) scheme(lean1)

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig5a.png", as(png) width(6400) height(4000) replace

hist pvalue, xlab(0.001 0.01 0.05 0.1, grid) ylab(0 10 20 30 40 50,grid) percent xtitle("P-values for electoral democracy index") xscale(r(., 0.1)) xline(0.05, lcolor(red)) xline(0.01, lcolor(red)) xline(0.001, lcolor(red)) ytitle("Percent of p-values (%)") aspectratio(1) scheme(lean1) yscale(r(., 50))

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig5b.png", as(png) width(6400) height(4000) replace

cap erase spec_curve.dta 

restore	
	
********************************************************************************
*Figure 6 - Bayesian model averaging
********************************************************************************

bmaregress $dv $polyarchy $controls i.year i.e_regionpol_id, rseed(12345)

bmagraph varmap, equalwidths poscoefopts(color(red%50)) negcoefopts(color(blue%50)) scheme(lean1) ylabel(19 "Electoral democracy" 18 "Saudi Arabia" 17 "Cultural similarity" 16 "Year - 2015" 15 "Region - LA" 14 "Year - 2017" 13 "Year - 2003" 12 "Head of state" 11 "GDP per capita (log10)" 10 "Region - SSA" 9 "Region - WE & NA" 8 "Region - SA" 7 "NATO member" 6 "Year - 2009" 5 "President abroad" 4 "Year - 2005" 3 "Trump" 2 "Region - SEA" 1 "Region - EA", labsize(small))

graph export "C:\Users\neilke\Dropbox\gifts\RIO_replication\figures\fig6.png", as(png) width(6400) height(4000) replace

		
********************************************************************************
*Figure 7 - sensitivity plots - NB. Have to go into graph editor to suppress some of the
*features and add the variable labels to the points
********************************************************************************

sensemakr $dv $polyarchy $controls i.year i.e_regionpol_id, treat($polyarchy) benchmark(e_migdppcln) tcontourplot clim(0 0.1) 



********************************************************************************
*Table 2 - Hurdle model
********************************************************************************
*set hurdle at max U.S. gift
gen ln_value_adj_hurdle = ln_value_adj
di log(2000)
replace ln_value_adj_hurdle = 0 if ln_value_adj_hurdle < 7.6009025

churdle linear ln_value_adj_hurdle $polyarchy $controls, select($polyarchy $controls) ll(7.6) vce(cluster country_id) cformat(%9.2f)
		
		
********************************************************************************
*Table 3 - Causal mediation analysis
********************************************************************************

gen binary_democracy = 1 if v2x_polyarchy >= .5
recode binary_democracy (miss=0)		

mediate ($dv v2xlg_legcon v2pepwrses v2x_corr $controls, linear) (v2mecrit v2xlg_legcon v2pepwrses v2x_corr $controls, linear) (binary_democracy), cformat(%9.2f)
estat proportion, cformat(%9.2f)

********************************************************************************
*Footnote 9, P.21 - Different types of autocratic regimes using Geddes et al
*from https://datafinder.qog.gu.se/datasource/gwf
********************************************************************************

preserve
import excel "C:\Users\neilke\Dropbox\gifts\RIO_replication\GWF Autocratic Regimes.xlsx", sheet("TSCS data") firstrow clear

keep if inrange(year, 2001, 2018)

gen country_name = gwf_country

*standardize names for merging
replace country_name = "Central African Republic" if country_name == "Cen African Rep"
replace country_name = "Republic of the Congo" if country_name == "Congo-Brz"
replace country_name = "Democratic Republic of the Congo" if country_name == "Congo/Zaire"
replace country_name = "Guinea-Bissau" if country_name == "Guinea Bissau"
replace country_name = "North Korea" if country_name == "Korea North"
replace country_name = "Burma/Myanmar" if country_name == "Myanmar"
replace country_name = "The Gambia" if country_name == "Gambia"

save gwf_autocratic_regimes, replace
restore 

*NB. only 287 matched
merge m:1 country_name year using gwf_autocratic_regimes
cap drop _merge

*make categorical
encode gwf_regimetype, gen(gwf_regimetype_cat)

*subsetting to autocratic regime - setting party-based as reference category
reghdfe $dv ib4.gwf_regimetype_cat $controls if binary_democracy == 0, absorb(year e_regionpol) vce(cluster country_id gift_giver_id) cformat(%9.2f)



log close
